home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / iparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-04  |  23.8 KB  |  818 lines

  1. /* Copyright (C) 1993, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* iparam.c */
  20. /* Interpreter implementations of parameter dictionaries */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "opcheck.h"
  26. #include "ialloc.h"
  27. #include "idict.h"
  28. #include "imemory.h"            /* for iutil.h */
  29. #include "iname.h"
  30. #include "istack.h"
  31. #include "iparam.h"
  32. #include "iutil.h"            /* for num_params */
  33. #include "ivmspace.h"
  34. #include "store.h"
  35.  
  36. /* ================ Utilities ================ */
  37.  
  38. #define iplist ((iparam_list *)plist)
  39. #define ciplist ((const iparam_list *)plist)
  40.  
  41. /* Convert a key to a ref. */
  42. private int near
  43. ref_param_key(const iparam_list *plist, gs_param_name pkey, ref *pkref)
  44. {    if ( plist->int_keys )
  45.       {    long key;
  46.         if ( sscanf(pkey, "%ld", &key) != 1 )
  47.           return_error(e_rangecheck);
  48.         make_int(pkref, key);
  49.         return 0;
  50.       }
  51.     else
  52.       {    return name_ref((const byte *)pkey, strlen(pkey), pkref, 0);
  53.       }
  54. }
  55.  
  56. /* ================ Writing parameters to refs ================ */
  57.  
  58. /* ---------------- Generic writing procedures ---------------- */
  59.  
  60. private param_proc_xmit_null(ref_param_write_null);
  61. private param_proc_xmit_bool(ref_param_write_bool);
  62. private param_proc_xmit_int(ref_param_write_int);
  63. private param_proc_xmit_long(ref_param_write_long);
  64. private param_proc_xmit_float(ref_param_write_float);
  65. private param_proc_xmit_string(ref_param_write_string);
  66. private param_proc_xmit_name(ref_param_write_name);
  67. private param_proc_xmit_int_array(ref_param_write_int_array);
  68. private param_proc_xmit_float_array(ref_param_write_float_array);
  69. private param_proc_xmit_string_array(ref_param_write_string_array);
  70. private param_proc_xmit_name_array(ref_param_write_name_array);
  71. private param_proc_begin_xmit_dict(ref_param_begin_write_dict);
  72. private param_proc_end_xmit_dict(ref_param_end_write_dict);
  73. private param_proc_requested(ref_param_requested);
  74. private const gs_param_list_procs ref_write_procs = {
  75.     ref_param_write_null,
  76.     ref_param_write_bool,
  77.     ref_param_write_int,
  78.     ref_param_write_long,
  79.     ref_param_write_float,
  80.     ref_param_write_string,
  81.     ref_param_write_name,
  82.     ref_param_write_int_array,
  83.     ref_param_write_float_array,
  84.     ref_param_write_string_array,
  85.     ref_param_write_name_array,
  86.     ref_param_begin_write_dict,
  87.     ref_param_end_write_dict,
  88.     ref_param_requested
  89. };
  90. private int near ref_array_param_requested(P5(const gs_param_list *, gs_param_name, ref *, uint, client_name_t));
  91. private int near ref_param_write(P3(iparam_list *, gs_param_name, const ref *));
  92. private int near ref_param_write_string_value(P2(ref *,
  93.   const gs_param_string *));
  94. #define ref_param_write_name_value(pref, pvalue)\
  95.   name_ref((pvalue)->data, (pvalue)->size, pref,\
  96.        ((pvalue)->persistent ? 0 : 1))
  97.  
  98. private int
  99. ref_param_write_null(gs_param_list *plist, gs_param_name pkey)
  100. {    ref value;
  101.     make_null(&value);
  102.     return ref_param_write(iplist, pkey, &value);
  103. }
  104. private int
  105. ref_param_write_bool(gs_param_list *plist, gs_param_name pkey, bool *pvalue)
  106. {    ref value;
  107.     make_bool(&value, *pvalue);
  108.     return ref_param_write(iplist, pkey, &value);
  109. }
  110. private int
  111. ref_param_write_int(gs_param_list *plist, gs_param_name pkey, int *pvalue)
  112. {    ref value;
  113.     make_int(&value, *pvalue);
  114.     return ref_param_write(iplist, pkey, &value);
  115. }
  116. private int
  117. ref_param_write_long(gs_param_list *plist, gs_param_name pkey, long *pvalue)
  118. {    ref value;
  119.     make_int(&value, *pvalue);
  120.     return ref_param_write(iplist, pkey, &value);
  121. }
  122. private int
  123. ref_param_write_float(gs_param_list *plist, gs_param_name pkey, float *pvalue)
  124. {    ref value;
  125.     make_real(&value, *pvalue);
  126.     return ref_param_write(iplist, pkey, &value);
  127. }
  128. private int
  129. ref_param_write_string(gs_param_list *plist, gs_param_name pkey,
  130.   gs_param_string *pvalue)
  131. {    ref sref;
  132.     int code;
  133.  
  134.     if ( !ref_param_requested(plist, pkey) )
  135.       return 0;
  136.     code = ref_param_write_string_value(&sref, pvalue);
  137.     if ( code < 0 )
  138.       return code;
  139.     return ref_param_write(iplist, pkey, &sref);
  140. }
  141. private int
  142. ref_param_write_name(gs_param_list *plist, gs_param_name pkey,
  143.   gs_param_string *pvalue)
  144. {    ref nref;
  145.     int code;
  146.  
  147.     if ( !ref_param_requested(plist, pkey) )
  148.       return 0;
  149.     code = ref_param_write_name_value(&nref, pvalue);
  150.     if ( code < 0 )
  151.       return code;
  152.     return ref_param_write(iplist, pkey, &nref);
  153. }
  154. private int
  155. ref_param_write_int_array(gs_param_list *plist, gs_param_name pkey,
  156.   gs_param_int_array *pvalue)
  157. {    ref value;
  158.     const int *pdata = pvalue->data;
  159.     uint n = pvalue->size;
  160.     ref *pe;
  161.     int code;
  162.  
  163.     if ( (code = ref_array_param_requested(plist, pkey, &value, n,
  164.                 "ref_param_write_int_array")) <= 0 )
  165.       return code;
  166.     for ( pe = value.value.refs; n > 0; n--, pe++, pdata++ )
  167.       make_int_new(pe, *pdata);
  168.     return ref_param_write(iplist, pkey, &value);
  169. }
  170. private int
  171. ref_param_write_float_array(gs_param_list *plist, gs_param_name pkey,
  172.   gs_param_float_array *pvalue)
  173. {    ref value;
  174.     const float *pdata = pvalue->data;
  175.     uint n = pvalue->size;
  176.     int code;
  177.     ref *pe;
  178.  
  179.     if ( (code = ref_array_param_requested(plist, pkey, &value, n,
  180.                 "ref_param_write_float_array")) <= 0 )
  181.       return code;
  182.     for ( pe = value.value.refs; n > 0; n--, pe++, pdata++ )
  183.       make_real_new(pe, *pdata);
  184.     return ref_param_write(iplist, pkey, &value);
  185. }
  186. private int
  187. ref_param_write_string_array(gs_param_list *plist, gs_param_name pkey,
  188.   gs_param_string_array *pvalue)
  189. {    ref value;
  190.     const gs_param_string *pdata = pvalue->data;
  191.     uint n = pvalue->size;
  192.     int code;
  193.     ref *pe;
  194.  
  195.     if ( (code = ref_array_param_requested(plist, pkey, &value, n,
  196.                 "ref_param_write_string_array")) <= 0 )
  197.       return code;
  198.     for ( pe = value.value.refs; n > 0; n--, pe++, pdata++ )
  199.       {    code = ref_param_write_string_value(pe, pdata);
  200.         if ( code < 0 )
  201.           {    /* Don't bother trying to release memory. */
  202.             return code;
  203.           }
  204.       }
  205.     return ref_param_write(iplist, pkey, &value);
  206. }
  207. private int
  208. ref_param_write_name_array(gs_param_list *plist, gs_param_name pkey,
  209.   gs_param_string_array *pvalue)
  210. {    ref value;
  211.     const gs_param_string *pdata = pvalue->data;
  212.     uint n = pvalue->size;
  213.     int code;
  214.     ref *pe;
  215.  
  216.     if ( (code = ref_array_param_requested(plist, pkey, &value, n,
  217.                 "ref_param_write_name_array")) <= 0 )
  218.       return code;
  219.     for ( pe = value.value.refs; n > 0; n--, pe++, pdata++ )
  220.       {    code = ref_param_write_name_value(pe, pdata);
  221.         if ( code < 0 )
  222.           {    /* Don't bother trying to release memory. */
  223.             return code;
  224.           }
  225.       }
  226.     return ref_param_write(iplist, pkey, &value);
  227. }
  228. private int
  229. ref_param_begin_write_dict(gs_param_list *plist, gs_param_name pkey,
  230.   gs_param_dict *pvalue, bool int_keys)
  231. {    dict_param_list *dlist =
  232.       (dict_param_list *)ialloc_bytes(size_of(dict_param_list),
  233.                       "ref_param_begin_write_dict");
  234.     ref dref;
  235.     int code;
  236.  
  237.     if ( dlist == 0 )
  238.       return_error(e_VMerror);
  239.     code = dict_create(pvalue->size, &dref);
  240.     if ( code < 0 )
  241.       {    ifree_object(dlist, "ref_param_begin_write_dict");
  242.         return code;
  243.       }
  244.     pvalue->list = (gs_param_list *)dlist;
  245.     code = dict_param_list_write(dlist, &dref, NULL);
  246.     if ( code < 0 )
  247.       return code;
  248.     dlist->int_keys = int_keys;
  249.     return 0;
  250. }
  251. private int
  252. ref_param_end_write_dict(gs_param_list *plist, gs_param_name pkey,
  253.   gs_param_dict *pvalue)
  254. {    int code = ref_param_write(iplist, pkey,
  255.                    &((dict_param_list *)pvalue->list)->dict);
  256.     ifree_object(pvalue->list, "ref_param_end_write_dict");
  257.     return code;
  258. }
  259.  
  260. /* Check whether a given parameter was requested. */
  261. private bool
  262. ref_param_requested(const gs_param_list *plist, gs_param_name pkey)
  263. {    ref kref;
  264.     ref *ignore_value;
  265.     if ( !r_has_type(&ciplist->u.w.wanted, t_dictionary) )
  266.       return true;
  267.     if ( ref_param_key(ciplist, pkey, &kref) < 0 )
  268.       return true;        /* catch it later */
  269.     return (dict_find(&ciplist->u.w.wanted, &kref, &ignore_value) > 0);
  270. }
  271. /* Check whether an array parameter is wanted, and allocate it if so. */
  272. /* Return <0 on error, 0 if not wanted, 1 if wanted. */
  273. private int near
  274. ref_array_param_requested(const gs_param_list *plist, gs_param_name pkey,
  275.   ref *pvalue, uint size, client_name_t cname)
  276. {    int code;
  277.     if ( !ref_param_requested(plist, pkey) )
  278.       return 0;
  279.     code = ialloc_ref_array(pvalue, a_all, size, cname);
  280.     return (code < 0 ? code : 1);
  281. }
  282.  
  283. /* ---------------- Internal routines ---------------- */
  284.  
  285. /* Prepare to write a string value. */
  286. private int near
  287. ref_param_write_string_value(ref *pref, const gs_param_string *pvalue)
  288. {    const byte *pdata = pvalue->data;
  289.     uint n = pvalue->size;
  290.  
  291.     if ( pvalue->persistent )
  292.         make_const_string(pref, a_readonly | avm_foreign, n, pdata);
  293.     else
  294.     {    byte *pstr = ialloc_string(n, "ref_param_write_string");
  295.         if ( pstr == 0 )
  296.           return_error(e_VMerror);
  297.         memcpy(pstr, pdata, n);
  298.         make_string(pref, a_readonly | icurrent_space, n, pstr);
  299.     }
  300.     return 0;
  301. }
  302.  
  303. /* Generic routine for writing a ref parameter. */
  304. private int near
  305. ref_param_write(iparam_list *plist, gs_param_name pkey, const ref *pvalue)
  306. {    ref kref;
  307.     int code;
  308.     if ( !ref_param_requested((gs_param_list *)plist, pkey) )
  309.       return 0;
  310.     code = ref_param_key(plist, pkey, &kref);
  311.     if ( code < 0 )
  312.       return code;
  313.     return (*plist->u.w.write)(plist, &kref, pvalue);
  314. }
  315.  
  316. /* ---------------- Implementations ---------------- */
  317.  
  318. /* Initialize for writing parameters. */
  319. private void near
  320. ref_param_write_init(iparam_list *plist, const ref *pwanted)
  321. {    if ( pwanted == 0 )
  322.       make_null(&plist->u.w.wanted);
  323.     else
  324.       plist->u.w.wanted = *pwanted;
  325.     plist->results = 0;
  326.     plist->int_keys = false;
  327. }
  328.  
  329. /* Implementation for getting parameters to a stack. */
  330. private int
  331. stack_param_write(iparam_list *plist, const ref *pkey, const ref *pvalue)
  332. {
  333. #define splist ((stack_param_list *)plist)
  334.     ref_stack *pstack = splist->pstack;
  335.     s_ptr p = pstack->p;
  336.     if ( pstack->top - p < 2 )
  337.       { int code = ref_stack_push(pstack, 2);
  338.         if ( code < 0 )
  339.           return code;
  340.         *ref_stack_index(pstack, 1) = *pkey;
  341.         p = pstack->p;
  342.       }
  343.     else
  344.       { pstack->p = p += 2;
  345.         p[-1] = *pkey;
  346.       }
  347.     *p = *pvalue;
  348.     splist->count++;
  349. #undef splist
  350.     return 0;
  351. }
  352. int
  353. stack_param_list_write(stack_param_list *plist, ref_stack *pstack,
  354.   const ref *pwanted)
  355. {    plist->procs = &ref_write_procs;
  356.     plist->u.w.write = stack_param_write;
  357.     ref_param_write_init((iparam_list *)plist, pwanted);
  358.     plist->pstack = pstack;
  359.     /* plist->skip not used */
  360.     plist->count = 0;
  361.     return 0;
  362. }    
  363.  
  364. /* Implementation for getting parameters to a dictionary. */
  365. private int
  366. dict_param_write(iparam_list *plist, const ref *pkey, const ref *pvalue)
  367. {    int code = dict_put(&((dict_param_list *)plist)->dict, pkey, pvalue);
  368.     return min(code, 0);
  369. }
  370. int
  371. dict_param_list_write(dict_param_list *plist, ref *pdict,
  372.   const ref *pwanted)
  373. {    check_dict_write(*pdict);
  374.     plist->procs = &ref_write_procs;
  375.     plist->u.w.write = dict_param_write;
  376.     ref_param_write_init((iparam_list *)plist, pwanted);
  377.     plist->dict = *pdict;
  378.     return 0;
  379. }    
  380.  
  381. /* ================ Reading refs to parameters ================ */
  382.  
  383. /* ---------------- Generic reading procedures ---------------- */
  384.  
  385. private param_proc_xmit_null(ref_param_read_null);
  386. private param_proc_xmit_bool(ref_param_read_bool);
  387. private param_proc_xmit_int(ref_param_read_int);
  388. private param_proc_xmit_long(ref_param_read_long);
  389. private param_proc_xmit_float(ref_param_read_float);
  390. private param_proc_xmit_string(ref_param_read_string);
  391. private param_proc_xmit_int_array(ref_param_read_int_array);
  392. private param_proc_xmit_float_array(ref_param_read_float_array);
  393. private param_proc_xmit_string_array(ref_param_read_string_array);
  394. private param_proc_begin_xmit_dict(ref_param_begin_read_dict);
  395. private param_proc_end_xmit_dict(ref_param_end_read_dict);
  396. private param_proc_get_policy(ref_param_read_get_policy);
  397. private param_proc_signal_error(ref_param_read_signal_error);
  398. private param_proc_commit(ref_param_read_commit);
  399. private const gs_param_list_procs ref_read_procs = {
  400.     ref_param_read_null,
  401.     ref_param_read_bool,
  402.     ref_param_read_int,
  403.     ref_param_read_long,
  404.     ref_param_read_float,
  405.     ref_param_read_string,
  406.     ref_param_read_string,        /* name = string */
  407.     ref_param_read_int_array,
  408.     ref_param_read_float_array,
  409.     ref_param_read_string_array,
  410.     ref_param_read_string_array,    /* name = string */
  411.     ref_param_begin_read_dict,
  412.     ref_param_end_read_dict,
  413.     NULL,                /* requested */
  414.     ref_param_read_get_policy,
  415.     ref_param_read_signal_error,
  416.     ref_param_read_commit
  417. };
  418. private int near ref_param_read(P4(iparam_list *, gs_param_name,
  419.   iparam_loc *, int));
  420. private int near ref_param_read_string_value(P2(const iparam_loc *,
  421.   gs_param_string *));
  422. private int near ref_param_read_array(P3(iparam_list *, gs_param_name,
  423.   iparam_loc *));
  424. #define iparam_return_error(loc, code)\
  425.   return_error(*(loc).presult = code)
  426. #define iparam_check_type(loc, typ)\
  427.   if ( !r_has_type((loc).pvalue, typ) )\
  428.     iparam_return_error(loc, e_typecheck)
  429. #define iparam_check_read(loc)\
  430.   if ( !r_has_attr((loc).pvalue, a_read) )\
  431.     iparam_return_error(loc, e_invalidaccess)
  432.  
  433. private int
  434. ref_param_read_null(gs_param_list *plist, gs_param_name pkey)
  435. {    iparam_loc loc;
  436.     return ref_param_read(iplist, pkey, &loc, t_null);
  437. }
  438. private int
  439. ref_param_read_bool(gs_param_list *plist, gs_param_name pkey, bool *pvalue)
  440. {    iparam_loc loc;
  441.     int code = ref_param_read(iplist, pkey, &loc, t_boolean);
  442.     if ( code != 0 )
  443.       return code;
  444.     *pvalue = loc.pvalue->value.boolval;
  445.     return 0;
  446. }
  447. private int
  448. ref_param_read_int(gs_param_list *plist, gs_param_name pkey, int *pvalue)
  449. {    iparam_loc loc;
  450.     int code = ref_param_read(iplist, pkey, &loc, t_integer);
  451.     if ( code != 0 )
  452.       return code;
  453. #if arch_sizeof_int < arch_sizeof_long
  454.     if ( loc.pvalue->value.intval != (int)loc.pvalue->value.intval )
  455.       return_error(e_rangecheck);
  456. #endif
  457.     *pvalue = (int)loc.pvalue->value.intval;
  458.     return 0;
  459. }
  460. private int
  461. ref_param_read_long(gs_param_list *plist, gs_param_name pkey, long *pvalue)
  462. {    iparam_loc loc;
  463.     int code = ref_param_read(iplist, pkey, &loc, t_integer);
  464.     if ( code != 0 )
  465.       return code;
  466.     *pvalue = loc.pvalue->value.intval;
  467.     return 0;
  468. }
  469. private int
  470. ref_param_read_float(gs_param_list *plist, gs_param_name pkey, float *pvalue)
  471. {    iparam_loc loc;
  472.     int code = ref_param_read(iplist, pkey, &loc, -1);
  473.     if ( code != 0 )
  474.       return code;
  475.     switch ( r_type(loc.pvalue) )
  476.     {
  477.     case t_integer:
  478.         *pvalue = loc.pvalue->value.intval;
  479.         break;
  480.     case t_real:
  481.         *pvalue = loc.pvalue->value.realval;
  482.         break;
  483.     default:
  484.         iparam_return_error(loc, e_typecheck);
  485.     }
  486.     return 0;
  487. }
  488. private int
  489. ref_param_read_string(gs_param_list *plist, gs_param_name pkey,
  490.   gs_param_string *pvalue)
  491. {    iparam_loc loc;
  492.     int code = ref_param_read(iplist, pkey, &loc, -1);
  493.     if ( code != 0 )
  494.       return code;
  495.     return ref_param_read_string_value(&loc, pvalue);
  496. }
  497. private int
  498. ref_param_read_int_array(gs_param_list *plist, gs_param_name pkey,
  499.   gs_param_int_array *pvalue)
  500. {    iparam_loc loc;
  501.     int code = ref_param_read_array(iplist, pkey, &loc);
  502.     int *piv;
  503.     uint size;
  504.     uint i;
  505.  
  506.     if ( code != 0 )
  507.       return code;
  508.     size = r_size(loc.pvalue);
  509.     piv = (int *)ialloc_byte_array(size, sizeof(int),
  510.                        "ref_param_read_int_array");
  511.     if ( piv == 0 )
  512.       return_error(e_VMerror);
  513.     for ( i = 0; i < size; i++ )
  514.     {    const ref *pe = loc.pvalue->value.const_refs + i;
  515.         if ( !r_has_type(pe, t_integer) )
  516.           { code = gs_note_error(e_typecheck); break; }
  517. #if arch_sizeof_int < arch_sizeof_long
  518.         if ( pe->value.intval != (int)pe->value.intval )
  519.           { code = gs_note_error(e_rangecheck); break; }
  520. #endif
  521.         piv[i] = (int)pe->value.intval;
  522.     }
  523.     if ( code < 0 )
  524.       {    ifree_object(piv, "ref_param_read_int_array");
  525.         return (*loc.presult = code);
  526.       }
  527.     pvalue->data = piv;
  528.     pvalue->size = size;
  529.     pvalue->persistent = true;
  530.     return 0;
  531. }
  532. private int
  533. ref_param_read_float_array(gs_param_list *plist, gs_param_name pkey,
  534.   gs_param_float_array *pvalue)
  535. {    iparam_loc loc;
  536.     int code = ref_param_read_array(iplist, pkey, &loc);
  537.     float *pfv;
  538.     uint size;
  539.  
  540.     if ( code != 0 )
  541.       return code;
  542.     size = r_size(loc.pvalue);
  543.     pfv = (float *)ialloc_byte_array(size, sizeof(float),
  544.                      "ref_param_read_float_array");
  545.     if ( pfv == 0 )
  546.       return_error(e_VMerror);
  547.     code = num_params(loc.pvalue->value.const_refs + size - 1, size, pfv);
  548.     if ( code < 0 )
  549.     {    ifree_object(pfv, "ref_read_float_array_param");
  550.         return (*loc.presult = code);
  551.     }
  552.     pvalue->data = pfv;
  553.     pvalue->size = size;
  554.     pvalue->persistent = true;
  555.     return 0;
  556. }
  557. private int
  558. ref_param_read_string_array(gs_param_list *plist, gs_param_name pkey,
  559.   gs_param_string_array *pvalue)
  560. {    iparam_loc loc;
  561.     int code = ref_param_read_array(iplist, pkey, &loc);
  562.     gs_param_string *psv;
  563.     ref *prefs;
  564.     uint size;
  565.     uint i;
  566.  
  567.     if ( code != 0 )
  568.       return code;
  569.     prefs = loc.pvalue->value.refs;
  570.     size = r_size(loc.pvalue);
  571.     psv = (gs_param_string *)ialloc_byte_array(size, sizeof(gs_param_string),
  572.                            "ref_param_read_string_array");
  573.     if ( psv == 0 )
  574.       return_error(e_VMerror);
  575.     for ( i = 0; code >= 0 && i < size; i++ )
  576.     {    loc.pvalue = prefs + i;
  577.         code = ref_param_read_string_value(&loc, psv + i);
  578.     }
  579.     if ( code < 0 )
  580.     {    ifree_object(psv, "ref_param_read_string_array");
  581.         return (*loc.presult = code);
  582.     }
  583.     pvalue->data = psv;
  584.     pvalue->size = size;
  585.     pvalue->persistent = true;
  586.     return 0;
  587. }
  588. private int
  589. ref_param_begin_read_dict(gs_param_list *plist, gs_param_name pkey,
  590.   gs_param_dict *pvalue, bool int_keys)
  591. {    iparam_loc loc;
  592.     dict_param_list *dlist =
  593.       (dict_param_list *)ialloc_bytes(size_of(dict_param_list),
  594.                       "ref_param_begin_write_dict");
  595.     int code = ref_param_read(iplist, pkey, &loc, t_dictionary);
  596.  
  597.     if ( code != 0 )
  598.       return code;
  599.     code = dict_param_list_read(dlist, loc.pvalue, NULL, false);
  600.     if ( code < 0 )
  601.       iparam_return_error(loc, code);
  602.     dlist->int_keys = int_keys;
  603.     pvalue->list = (gs_param_list *)dlist;
  604.     pvalue->size = dict_length(loc.pvalue);
  605.     return 0;
  606. }
  607. private int
  608. ref_param_end_read_dict(gs_param_list *plist, gs_param_name pkey,
  609.   gs_param_dict *pvalue)
  610. {    iparam_list_release((dict_param_list *)pvalue->list);
  611.     ifree_object(pvalue->list, "ref_param_end_read_dict");
  612.     return 0;
  613. }
  614. private int
  615. ref_param_read_get_policy(gs_param_list *plist, gs_param_name pkey)
  616. {    ref kname;
  617.     int code;
  618.     ref *pvalue;
  619.     /* We can't use dict_find_string directly here, because */
  620.     /* pkey might not be a _ds string. */
  621.     if ( !(r_has_type(&iplist->u.r.policies, t_dictionary) &&
  622.            (code = name_ref((const byte *)pkey, strlen(pkey), &kname, -1)) >= 0 &&
  623.            dict_find(&iplist->u.r.policies, &kname, &pvalue) > 0 &&
  624.            r_has_type(pvalue, t_integer))
  625.        )
  626.       return gs_param_policy_ignore;
  627.     return (int)pvalue->value.intval;
  628. }
  629. private int
  630. ref_param_read_signal_error(gs_param_list *plist, gs_param_name pkey, int code)
  631. {    iparam_loc loc;
  632.     ref_param_read(iplist, pkey, &loc, -1);    /* can't fail */
  633.     *loc.presult = code;
  634.     switch ( ref_param_read_get_policy(plist, pkey) )
  635.       {
  636.       case gs_param_policy_ignore:
  637.         return 0;
  638.       case gs_param_policy_consult_user:
  639.         return_error(e_configurationerror);
  640.       default:
  641.         return code;
  642.       }
  643. }
  644. private int
  645. ref_param_read_commit(gs_param_list *plist)
  646. {    int i;
  647.     int ecode = 0;
  648.     if ( !iplist->u.r.require_all )
  649.       return 0;
  650.     /* Check to make sure that all parameters were actually read. */
  651.     for ( i = 0; i < iplist->count; ++i )
  652.       if ( iplist->results[i] == 0 )
  653.         iplist->results[i] = ecode = gs_note_error(e_undefined);
  654.     return ecode;
  655. }
  656.  
  657. /* ---------------- Internal routines ---------------- */
  658.  
  659. /* Read a string value. */
  660. private int near
  661. ref_param_read_string_value(const iparam_loc *ploc, gs_param_string *pvalue)
  662. {    const ref *pref = ploc->pvalue;
  663.     ref nref;
  664.     switch ( r_type(pref) )
  665.     {
  666.     case t_name:
  667.         name_string_ref(pref, &nref);
  668.         pref = &nref;
  669.         pvalue->persistent = true;
  670.         goto s;
  671.     case t_string:
  672.         iparam_check_read(*ploc);
  673.         pvalue->persistent = false;
  674. s:        pvalue->data = pref->value.const_bytes;
  675.         pvalue->size = r_size(pref);
  676.         break;
  677.     default:
  678.         iparam_return_error(*ploc, e_typecheck);
  679.     }
  680.     return 0;
  681. }
  682.  
  683. /* Read an array parameter. */
  684. private int near
  685. ref_param_read_array(iparam_list *plist, gs_param_name pkey, iparam_loc *ploc)
  686. {    int code = ref_param_read(plist, pkey, ploc, t_array);
  687.     if ( code != 0 )
  688.       return code;
  689.     iparam_check_read(*ploc);
  690.     return 0;
  691. }
  692.  
  693. /* Generic routine for reading a ref parameter. */
  694. private int near
  695. ref_param_read(iparam_list *plist, gs_param_name pkey, iparam_loc *ploc,
  696.   int type)
  697. {    ref kref;
  698.     int code = ref_param_key(plist, pkey, &kref);
  699.     if ( code < 0 )
  700.       return code;
  701.     code = (*plist->u.r.read)(iplist, &kref, ploc);
  702.     if ( code != 0 )
  703.       return code;
  704.     if ( type >= 0 )
  705.       iparam_check_type(*ploc, type);
  706.     return 0;
  707. }
  708.  
  709. /* ---------------- Implementations ---------------- */
  710.  
  711. /* Initialize for reading parameters. */
  712. private int
  713. ref_param_read_init(iparam_list *plist, uint count, const ref *ppolicies,
  714.   bool require_all)
  715. {    if ( ppolicies == 0 )
  716.       make_null(&plist->u.r.policies);
  717.     else
  718.       plist->u.r.policies = *ppolicies;
  719.     plist->u.r.require_all = require_all;
  720.     plist->count = count;
  721.     plist->results =
  722.       (int *)ialloc_byte_array(count, sizeof(int), "ref_param_read_init");
  723.     if ( plist->results == 0 )
  724.       return_error(e_VMerror);
  725.     memset(plist->results, 0, count * sizeof(int));
  726.     plist->int_keys = false;
  727.     return 0;
  728. }
  729.  
  730. /* Implementation for putting parameters from an array. */
  731. private int
  732. array_param_read(iparam_list *plist, const ref *pkey, iparam_loc *ploc)
  733. {    ref *bot = ((array_param_list *)plist)->bot;
  734.     ref *ptr = bot;
  735.     ref *top = ((array_param_list *)plist)->top;
  736.     for ( ; ptr < top; ptr += 2 )
  737.     {    if ( r_has_type(ptr, t_name) && name_eq(ptr, pkey) )
  738.         {    ploc->pvalue = ptr + 1;
  739.             ploc->presult = &plist->results[ptr - bot];
  740.             *ploc->presult = 1;
  741.             return 0;
  742.         }
  743.     }
  744.     return 1;
  745. }
  746. int
  747. array_param_list_read(array_param_list *plist, ref *bot, uint count,
  748.   const ref *ppolicies, bool require_all)
  749. {    if ( count & 1 )
  750.       return_error(e_rangecheck);
  751.     plist->procs = &ref_read_procs;
  752.     plist->u.r.read = array_param_read;
  753.     plist->bot = bot;
  754.     plist->top = bot + count;
  755.     return ref_param_read_init(iplist, count, ppolicies, require_all);
  756. }
  757.  
  758. /* Implementation for putting parameters from a stack. */
  759. private int
  760. stack_param_read(iparam_list *plist, const ref *pkey, iparam_loc *ploc)
  761. {
  762. #define splist ((stack_param_list *)plist)
  763.     ref_stack *pstack = splist->pstack;
  764.     /* This implementation is slow, but it probably doesn't matter. */
  765.     uint index = splist->skip + 1;
  766.     uint count = splist->count;
  767.     for ( ; count; count--, index += 2 )
  768.       {    const ref *p = ref_stack_index(pstack, index);
  769.         if ( r_has_type(p, t_name) && name_eq(p, pkey) )
  770.           {    ploc->pvalue = ref_stack_index(pstack, index - 1);
  771.             ploc->presult = &plist->results[count - 1];
  772.             *ploc->presult = 1;
  773.             return 0;
  774.           }
  775.       }
  776. #undef splist
  777.     return 1;
  778. }
  779. int
  780. stack_param_list_read(stack_param_list *plist, ref_stack *pstack, uint skip,
  781.   const ref *ppolicies, bool require_all)
  782. {    uint count = ref_stack_counttomark(pstack);
  783.     if ( count == 0 )
  784.       return_error(e_unmatchedmark);
  785.     count -= skip + 1;
  786.     if ( count & 1 )
  787.       return_error(e_rangecheck);
  788.     plist->procs = &ref_read_procs;
  789.     plist->u.r.read = stack_param_read;
  790.     plist->pstack = pstack;
  791.     plist->skip = skip;
  792.     return ref_param_read_init(iplist, count >> 1, ppolicies, require_all);
  793. }    
  794.  
  795. /* Implementation for putting parameters from a dictionary. */
  796. private int
  797. dict_param_read(iparam_list *plist, const ref *pkey, iparam_loc *ploc)
  798. {
  799. #define spdict (&((dict_param_list *)plist)->dict)
  800.     int code = dict_find(spdict, pkey, &ploc->pvalue);
  801.     if ( code != 1 )
  802.       return 1;
  803.     ploc->presult = &plist->results[dict_value_index(spdict, ploc->pvalue)];
  804. #undef spdict
  805.     *ploc->presult = 1;
  806.     return 0;
  807. }
  808. int
  809. dict_param_list_read(dict_param_list *plist, const ref *pdict,
  810.   const ref *ppolicies, bool require_all)
  811. {    check_dict_read(*pdict);
  812.     plist->procs = &ref_read_procs;
  813.     plist->u.r.read = dict_param_read;
  814.     plist->dict = *pdict;
  815.     return ref_param_read_init(iplist, dict_max_index(pdict) + 1,
  816.                    ppolicies, require_all);
  817. }    
  818.